#include "core/main.h"

ParameterFile::ParameterFile(const string &filename)
{
    AddFile(filename);
}

void ParameterFile::AddFile(const string &filename)
{
    bool fileFound = Utility::FileExists(filename);
    Assert(fileFound, "Parameter file not found");
    
    vector<string> lines = Utility::GetFileLines(filename);
    for(UINT lineIndex = 0; lineIndex < lines.size(); lineIndex++)
    {
        string &curLine = lines[lineIndex];
        if(curLine.size() > 2 && curLine[0] != '#')
        {
            vector<string> partition = StringHelper::Partition(curLine, "=");
            Assert(partition.size() == 2, string("Invalid line in parameter file: ") + curLine);
            _parameters.push_back(ParameterEntry(partition[0], partition[1]));
        }
    }
}

ParameterEntry* ParameterFile::FindParameter(const string &parameterName)
{
    for(auto ParameterIterator = _parameters.begin(); ParameterIterator != _parameters.end(); ParameterIterator++)
    {
        auto &CurEntry = *ParameterIterator;
        if(CurEntry.name == parameterName)
        {
            return &CurEntry;
        }
    }
    return NULL;
}

const ParameterEntry* ParameterFile::FindParameter(const string &parameterName) const
{
    for(auto ParameterIterator = _parameters.begin(); ParameterIterator != _parameters.end(); ParameterIterator++)
    {
        auto &CurEntry = *ParameterIterator;
        if(CurEntry.name == parameterName)
        {
            return &CurEntry;
        }
    }
    return NULL;
}

string ParameterFile::GetRequiredString(const string &parameterName) const
{
    for(UINT i = 0; i < _parameters.size(); i++)
    {
        if(_parameters[i].name == parameterName)
        {
            return _parameters[i].value;
        }
    }
    Assert(false, string("Parameter not found: ") + parameterName);
    return string("Parameter not found");
}

string ParameterFile::GetOptionalString(const string &parameterName) const
{
    for(UINT i = 0; i < _parameters.size(); i++)
    {
        if(_parameters[i].name == parameterName)
        {
            return _parameters[i].value;
        }
    }
    return string();
}

bool ParameterFile::GetBoolean(const string &parameterName) const
{
    string str = GetRequiredString(parameterName);
    if(str == "true")
    {
        return true;
    }
    else if(str == "false")
    {
        return false;
    }
    else
    {
        Assert(false, string("Invalid boolean value: ") + str);
        return false;
    }
}

int ParameterFile::GetInteger(const string &parameterName) const
{
    return StringHelper::ToInteger(GetRequiredString(parameterName));
}

UINT ParameterFile::GetUnsignedInteger(const string &parameterName) const
{
    return StringHelper::ToUnsignedInteger(GetRequiredString(parameterName));
}

double ParameterFile::GetDouble(const string &parameterName) const
{
    return StringHelper::ToDouble(GetRequiredString(parameterName));
}

float ParameterFile::GetFloat(const string &parameterName) const
{
    return StringHelper::ToFloat(GetRequiredString(parameterName));
}
